package com.chijun.mform.process.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import com.chijun.mform.FormBuilder;
import com.chijun.mform.FormUtils;
import com.chijun.mform.R;
import com.chijun.mform.full.mode.ModeFormFullApprovePerson;
import com.chijun.mform.process.IHandlerLoadFormFull;
import com.chijun.mform.process.IWaitUploadCompleteListener;
import com.chijun.mform.process.IWritePingJiaCallBack;
import com.chijun.mform.process.ModeServerFullData;
import com.chijun.mform.process.TJPingLunView;
import com.example.ccj.b.c.custom.ImageAndTextButton;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.b.c.dialogxy.SYDialog;
import com.example.ccj.b.c.draw.TopBar;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.tengxun.cos.CosDeleteCallBack;


/**
 * 审批dialog
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/30 12:24
 */
public class DialogShenPi extends SYDialog.Builder {

    ModeServerFullData serverFullData;
    IWritePingJiaCallBack callBack;
    TJPingLunView pl;
    IHandlerLoadFormFull iHandlerLoadFormFull;

    public DialogShenPi(Context context, ModeServerFullData serverFullData, IHandlerLoadFormFull iHandlerLoadFormFull , IWritePingJiaCallBack callBack) {
        super(context);
        this.serverFullData = serverFullData;
        this.callBack = callBack;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;


        setDialogView(R.layout.dialog_view_shen_pi)
                .setTitle("审批")
                .setCancelableOutSide(false)
                .setCancelable(false)
                .setGravity(Gravity.BOTTOM)
                .setScreenHeightP(0.5F)
                .setScreenWidthP(1)
                .setWindowBackground(Color.WHITE);
    }

    ModeFormFullApprovePerson pingLun = null;
    @Override
    public void initView(View layout) {
        super.initView(layout);
        pl = layout.findViewById(R.id.dialog_view_shen_pi_view);
        ImageAndTextButton tongYi = layout.findViewById(R.id.dialog_view_shen_pi_ty);
        ImageAndTextButton jvJue = layout.findViewById(R.id.dialog_view_shen_pi_jj);

        TopBar topBar = layout.findViewById(R.id.dialog_view_shen_pi_topbar);
        topBar.setTitle("审批");
        topBar.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isNoEmpty(pl.imgStr)) {
                    String[] split = pl.imgStr.toString().split(",");
                    for (String s : split) {
                        FormUtils.cosDeleteFile(v.getContext(), s, new CosDeleteCallBack() {
                            @Override
                            public void onComplete(String s) {}
                            @Override
                            public void onError(String s) {}
                        });
                    }
                }
                dismiss();
            }
        });
        topBar.setRightText("取消").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isNoEmpty(pl.imgStr)) {
                    String[] split = pl.imgStr.toString().split(",");
                    for (String s : split) {
                        FormUtils.cosDeleteFile(v.getContext(), s, new CosDeleteCallBack() {
                            @Override
                            public void onComplete(String s) {}
                            @Override
                            public void onError(String s) {}
                        });
                    }
                }
                dismiss();
            }
        });

        pl.sateData(serverFullData,serverFullData.thisFormFullApprovePerson);

        tongYi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shenPi(2);
            }
        });

        jvJue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shenPi(3);
            }
        });
    }

    private void shenPi(int state) {
        if (serverFullData.launchedForm.state == 1 && serverFullData.isDoingPerson && serverFullData.thisFormFullApprovePerson != null) {
            if (serverFullData.thisFormFullApprovePerson.state == 1) {
                if (iHandlerLoadFormFull != null) {
                    LoadingDialog loadingDialog = new LoadingDialog(pl.getContext());
                    serverFullData.thisFormFullApprovePerson.state = state;
                    serverFullData.thisFormFullApprovePerson.opinion = pl.opinionStr;
                    serverFullData.thisFormFullApprovePerson.image = pl.imgStr.toString();
                    iHandlerLoadFormFull.uploadShenPi( serverFullData.thisFormFullApprovePerson, new IWaitUploadCompleteListener() {
                        @Override
                        public void onComplete() {
                            callBack.onSuccessSp();
                            loadingDialog.dismiss();
                            dismiss();
                        }

                        @Override
                        public void onError(String value) {
                            loadingDialog.dismiss();
                        }
                    });
                }
            }
        }
    }

}
